<?php

namespace App\Http\Requests\Auth;

use App\Http\Requests\AbstractRequest;
use App\Models\User;

class RegisterRequest extends AbstractRequest
{

    /**
     * 验证器规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|username',
            'mobile' => 'required|mobile',
            'password' => 'required|password|confirmed'
        ];
    }

    /**
     * 自定义验证
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validate()
    {
        if (User::where($this->only('name'))->exists()) {

            $this->validationException([
                'name' => ['用户名 已存在']
            ]);

        } elseif (User::where($this->only('mobile'))->exists()) {

            $this->validationException([
                'mobile' => ['手机号 已存在']
            ]);
        }
    }
}
